<template>
  <!-- 资讯列表页面 -->
  <div>
    <el-form :inline="true" :model="params" class="demo-form-inline">
      <el-form-item>
        <el-select v-model="params.status" placeholder="状态" clearable>
          <el-option label="未审核" value="未审核" />
          <el-option label="审核通过" value="审核通过" />
          <el-option label="审核不通过" value="审核不通过" />
          <el-option label="推荐" value="推荐" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input v-model="params.title" placeholder="标题" clearable />
      </el-form-item>
      <el-form-item>
        <el-select v-model="params.categoryId" placeholder="请选择栏目" clearable>
          <el-option
            v-for="item in categoryData"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="toAdd">添加商品</el-button>
      </el-form-item>      
    </el-form>
    <el-table
      :data="articleData"
      tooltip-effect="dark"
      style="width: 100%"
      :v-loading="loading"
    >
      <el-table-column
        type="index"
        label="序号"
        :index="1"
        width="100"
      />
      <el-table-column prop="cover" label="封面" width="160">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="right">
            <img slot="reference" :src="scope.row.cover" alt="加载失败" width="100px">
            <img width="200px" :src="scope.row.cover" alt="">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="风采" width="760">
        <template slot-scope="scope">
          <p>{{ scope.row.title }}</p>
          <p>{{ scope.row.readTimes }}</p>
          <p>{{ scope.row.category.name }}</p>
          <p>{{ scope.row.publishTime | fmtDate }}</p>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="100">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status == '审核通过'" type="primary" plain>审核通过</el-button>
          <el-button v-else-if="scope.row.status == '审核不通过'" type="danger" plain disabled>审核不通过</el-button>
          <el-button v-else-if="scope.row.status == '未审核'" type="info" plain>未审核</el-button>
          <el-button v-else type="success" plain>推荐</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <a href="#" style="color:aqua;margin-right: 15px;" @click.prevent="recommend(scope.row)">推荐</a>
          <a href="#" style="color:blue;margin-right: 15px;" @click.prevent="toTest(scope.row)">审核</a>
          <a href="#" style="color:coral" @click.prevent="toDelete(scope.row)">删除</a>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="page">
      <el-pagination
        :current-page="params.page"
        :page-sizes="[2, 4, 5, 8, , 20, 50]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 审核模态框 -->
    <el-dialog title="审核资讯信息" :visible.sync="visible" width="60%">
      <el-table
        :data="articleData"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column>
          <template slot-scope="scope">
            <span>{{ scope.row.content }}</span>
            <span slot="footer" class="dialog-footer">
              <el-button type="danger" @click="pass(scope.row)">审核不通过</el-button>
              <el-button type="primary" @click="good(scope.row)">审核通过</el-button>
            </span>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {
  deleteArticle,
  changeStatus,
  goSearch,
  findAll
} from '@/api/article'
import { queryCateConsult } from '@/api/infoCategory'
export default {
  data() {
    return {
      // // 资讯栏目数据
      categoryData: [],
      // // 分页查询咨询详情
      articleData: [],
      loading: false,
      total: 0,
      // 分页表单
      params: {
        page: 1,
        pageSize: 10
      },
      visible: false
    }
  },
  computed: {},
  created() {
    // this.getConsult();
    this.getColumn()
    this.getArticle()
  },
  mounted() {},
  methods: {
    // 推荐
    async recommend(row) {
      this.$confirm('你确定要推荐此资讯吗？', '提示', {
        confirmButtonText: '确定',
        cancleButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        this.$set(row, 'status', '推荐')
        console.log(row)
        await changeStatus(row)
        this.$message({
          type: 'success',
          message: '推荐成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    },
    // 审核
    toTest() {
      this.visible = true
    },
    // 审核通过
    async good(row) {
      this.$set(row, 'status', '审核通过')
      await changeStatus(row)
      this.visible = false
    },
    // 审核不通过
    async pass(row) {
      this.$set(row, 'status', '审核不通过')
      await changeStatus(row)
      this.visible = false
    },
    // 通过id删除
    async toDelete(row) {
      await deleteArticle(row)
      this.getArticle()
    },
    // 获取所有栏目名称
    async getColumn() {
      for (const key in this.params) {
        if (!this.params[key]) {
          delete this.params[key]
        }
      }
      const res = await queryCateConsult(this.params)
      this.categoryData = res.data
    },
    // 查询 
    async onSubmit() {
      this.params.page = 1
      this.getArticle();  
    },
    // 分页查询咨询信息
    async getArticle() {
      this.loading = true
      for (const key in this.params) {
        if (!this.params[key]) {
          delete this.params[key]
        }
      }
      const res = await goSearch(this.params)
      this.articleData = res.data.list
      // console.log(res.data.list);
      this.total = res.data.total
      this.loading = false
    },
    // 页码条数发生改变的方法
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize
      this.params.page = 1
      this.getArticle()
    },
    // 当前页码改变触发的方法
    handleCurrentChange(page) {
      this.params.page = page
      this.getArticle()
    },
    // 级联查询所有信息 articleData
    async getArticles() {
      for (const key in this.params) {
        if (!this.params[key]) {
          delete this.params[key]
        }
      }
      const res = await findAll(this.params)
      this.categoryData = res.data.list
      console.log(res.data.list)
      this.total = res.data.total
    },
    // 发布商品
    toAdd(){
      this.$router.push({
        path:'Publish'
      })
    }
  }
}
</script>
<style scoped>
</style>
